<?php
class Board {
	private $cells = array();
	
	public function __construct($cell_array)
	{
        $this->cells = $cell_array;		
	}
	
	/**
	 * This will return the total number of cells.
	 * @return total number of cells
	 */
	public function getNumberOfCells(){
		
		return count($this->cells);
	}
	
	/**
	 * returns the cell in the cells array based upon the index.
	 * @param object $index
	 * @return The cell at position index
	 */
	public function getCellByIndex($index)
	{
		return $this->cells[$index];
	}

	/**
	 * returns the name of the cell from the cells array based upon the index.
	 * @param object $index
	 * @return The name of the cell at position index
	 */
	public function getCellNameByIndex($index)
	{
		$cell = $this->getCellByIndex($index);
		if($cell != NULL)
		{
			return $cell->getName();
		}
	}
	
	/**
	 * Returns the cell with the given name.
	 * @param object $name
	 * @return the cell that has the given name.
	 */
	public function getCellByName($name) {
		foreach($this->cells as $cell) {
			if($cell->getName() == $name) {
				return $cell;
			}
		}
		return NULL;
	}
	
	/**
	 * Gets all the cells that have the $color as there color.
	 * @param object $color
	 * @return an array of all cells with color of $color
	 */
	public function getCellsByColor($color)
	{
		$color_array = array();
		foreach($this->cells as $cell)
		{
			if($cell->getColor() == $color)
			{
				$color_array[] = $cell;
			}
		}
		return $color_array;
	}
	
	/**
	 * This will return the index of the new locaiton. It is important because after 39 it starts at 0 again.
	 * @param object $current
	 * @param object $movement
	 * @return The index of the new location.
	 */
	public function getNewLocationIndex($current, $movement)
	{
		$total_cells = count($this->cells);
		$newLocation = $current + $movement;
		if($newLocation > $total_cells - 1)
		{
			$newLocation = $newLocation - $total_cells;
		}
		return $newLocation;	
	}
	
	/**
	 * Returns the cell at the given location
	 * @param object $current_cell
	 * @param object $movement
	 * @return 
	 */
	public function getNewLocationCell($current_cell, $movement)
	{
		$cell_index = $current_cell->getPosition();
		$new_index = $this->getNewLocationIndex($cell_index, $movement);
		
		return $this->cells[$new_index];		
	}

	/**
	 * This will determine if the specified player owns all cell of a given color.
	 * @param object $player
	 * @param object $color
	 * @return True if player owns all, otherwise false.
	 */
	public function playerOwnsAllColorCell($player, $color)
	{
		$color_array = $this->getCellsByColor($color);
		$ownsAll = true;
		foreach($color_array as $c_cell)
		{
			if($player != $c_cell->getOwner() && !$c_cell->isMortgaged())
			{
				$ownsAll = false;
			}	
		}
		return $ownsAll;
	}
	
	/**
	 * Returns an array of cells that are of the given type.
	 * @param object $type
	 * @return An array of all cells of the given type.
	 */
	public function getCellsByType($type)
	{
		$result_array = array();
		foreach($this->cells as $cell)
		{
			if($cell->getType() == $type)
			{
				$result_array[] = $cell;
			}
		}
		return $result_array;
	}
}